﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SharpPcap;
using SharpPcap.Packets;
using SharpPcap.Util;

namespace DotaStats
{
    /// <summary>
    /// Interaction logic for PreferencesWindow.xaml
    /// </summary>
    public partial class PreferencesWindow : Window
    {
        String captureDeviceDescription = String.Empty;
        List<PcapDevice> captureDevices = Pcap.GetAllDevices();

        //public static DependencyProperty PortProperty = DependencyProperty.Register(
        //    "GamePort",
        //    typeof(Int16),
        //    typeof(Window),
        //    new PropertyMetadata(6112)
        //   );

        //public Int16 Port
        //{
        //    get { return (Int16)this.GetValue(PortProperty); }
        //    set { this.SetValue(PortProperty, value); }
        //}

        public String CaptureDeviceDescription
        {
            get { return captureDeviceDescription; }
        }

        public PreferencesWindow()
        {
            InitializeComponent();

            foreach (PcapDevice device in captureDevices)
                comboBox1.Items.Add(device.Description);

            comboBox1.Text = Properties.Settings.Default["CaptureDevice"].ToString();
            textBox1.Text = Properties.Settings.Default["BattleNetPort"].ToString();

            checkBox1.IsChecked = (bool)Properties.Settings.Default["SaveReplay"];
            checkBox2.IsChecked = (bool)Properties.Settings.Default["SavePackets"];
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EnableApplyButton();
        }

        private void EnableApplyButton()
        {
            button4.IsEnabled =
                button3.IsEnabled =
                button2.IsEnabled =
                button1.IsEnabled = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                captureDeviceDescription = comboBox1.SelectedValue.ToString();
                Properties.Settings.Default["CaptureDevice"] = captureDeviceDescription;
            }

            Properties.Settings.Default["SaveReplay"] = checkBox1.IsChecked;
            Properties.Settings.Default["SavePackets"] = checkBox2.IsChecked;

            Properties.Settings.Default["BattleNetPort"] = Int32.Parse(textBox1.Text);

            Properties.Settings.Default.Save();
            DisableApplyButton();
        }

        private void DisableApplyButton()
        {
            button4.IsEnabled =
                button3.IsEnabled =
                button2.IsEnabled =
                button1.IsEnabled = false;
        }
    }
}
